package org.carabiner.util;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Properties object for the carabiner framework. This class reads it's
 * properties from the <code>carabiner.properties</code> file in the current
 * working directory.
 * 
 * @author Ben Rady (benrady@gmail.com)
 * 
 */
public class CarabinerProperties
{
  static final String PROPERTIES_FILE = "carabiner.properties";

  private static Properties properties;

  private CarabinerProperties()
  {
  }

  private static Properties getProperties()
  {
    if (properties == null)
    {
      File file = new File(PROPERTIES_FILE);
      properties = new Properties();
      if (file.exists())
      {
        InputStream stream = null;
        try
        {
          stream = new FileInputStream(file);
          properties.load(stream);
        } catch (IOException ex)
        {
          Logger.getLogger(CarabinerProperties.class.getName()).log(
              Level.WARNING,
              "Error loading Carabiner properties file at "
                  + file.getAbsolutePath(), ex);
        } finally
        {
          try
          {
            stream.close();
          } catch (IOException ex)
          {
            ex.printStackTrace();
          }
        }
      }
    }
    return properties;
  }

  private static final String STATE_FACTORY_CLASS_NAME = "StateFactoryClassName";

  /**
   * The fully qualified class name of the
   * {@link org.carabiner.state.StateFactory} that should be used with
   * Carabiner. The default value is
   * <code>org.carabiner.state.XMLStateFactory</code><p/>
   * 
   * Defined by the <code>StateFactoryClassName</code> property
   * 
   * @return String
   */
  public static String getStateFactoryClassName()
  {
    return getProperties().getProperty(STATE_FACTORY_CLASS_NAME,
        "org.carabiner.state.XMLStateFactory");
  }

  private static final String STATE_FILE_NAME = "StateFileName";

  /**
   * The filename of the xml file that carabiner stores bless/curse information
   * in. Defaults to <code>carabiner.xml</code><p/>
   * 
   * Defined by the <code>StateFileName</code> property
   * 
   * @return String
   */
  public static String getStateFile()
  {
    return getProperties().getProperty(STATE_FILE_NAME, "carabiner.xml");
  }

  private static String STATE_STORAGE_DTD_URL = "StateStorageDTD";

  /**
   * The URL of the location where the statestorage DTD can be found. For
   * example:
   * jar:file:/java/lib/carabiner.jar!/org/carabiner/state/statestorage.dtd<p/>
   * 
   * Defined by the <code>StateStorageDTD</code> property. If this value is
   * null, which it is by default, the state storage XML file will not be
   * validated.
   * 
   * @return String
   */
  public static String getDtdURL()
  {
    return getProperties().getProperty(STATE_STORAGE_DTD_URL);
  }

  /**
   * Gets the color used to highlight components in the Component Viewer. <p/>
   * 
   * Defining this color in the properties file is not currently supported.
   * 
   * @return Color
   */
  public static Color getDefaultComponentHighlightColor()
  {
    return Color.blue;
  }

  private static String INSPECTOR_LIST_FILE = "InspectorListFile";

  /**
   * Returns the filename of the list of {@link org.carabiner.infopanel.Rule
   * Rules} that the inspector should use when inspecting components. This file
   * should be a plain text file with a single rule (specified by a fully
   * qualified class name) on each line. By default, the file name is
   * 'carabiner.rules'. If this file not not exist, the default rule set will be
   * used. <p/>
   * 
   * Defined by the <code>InspectorListFile</code> property
   * 
   * @return String
   */
  public static String getInspectorListFilename()
  {
    return getProperties().getProperty(INSPECTOR_LIST_FILE, "carabiner.rules");
  }
}
